package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.pojo.PageQuery;
import com.cf.domain.pojo.TeamMember;
import com.cf.domain.vo.TeamVO;
import com.cf.domain.vo.UserVO;
import com.cf.enums.UserRole;
import com.cf.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
public class ExcelController {

    private final IUserService userService;

    @GetMapping("/excel")
    @ResponseBody
    public ResponseEntity<byte[]> exportExcel(@RequestParam(name = "areaSearch", required = false) String areaSearch,
                                              HttpSession session) throws IOException {
        UserVO userVO = (UserVO) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        if(userVO.getUserRole()==UserRole.ADMIN.getId()){
            if(areaSearch == null){
                areaSearch = "0";
            }
        }else if(userVO.getUserRole()==UserRole.REGIONADMIN.getId()){
            areaSearch = String.valueOf(userVO.getArea());
        }
        map.put("areaSearch", areaSearch);
        List<TeamVO> data = userService.selectMembers(map, userVO);
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("ExampleSheet");

        // 创建标题行
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("姓名");

        titleCell = titleRow.createCell(1);
        titleCell.setCellValue("手机号");

        titleCell = titleRow.createCell(2);
        titleCell.setCellValue("邮箱");

        titleCell = titleRow.createCell(3);
        titleCell.setCellValue("赛道");

        titleCell = titleRow.createCell(4);
        titleCell.setCellValue("是否辅导");

        titleCell = titleRow.createCell(5);
        titleCell.setCellValue("团队名称");

        titleCell = titleRow.createCell(6);
        titleCell.setCellValue("参赛地域");

        titleCell = titleRow.createCell(7);
        titleCell.setCellValue("来源");

        titleCell = titleRow.createCell(8);
        titleCell.setCellValue("团队人数");

        titleCell = titleRow.createCell(9);
        titleCell.setCellValue("团队成员");

        // 填充数据
        for (int i = 0; i < data.size(); i++) {
            Row row = sheet.createRow(i + 1);
            Cell cell = row.createCell(0);
            cell.setCellValue(data.get(i).getName());

            cell = row.createCell(1);
            cell.setCellValue(data.get(i).getPhone());

            cell = row.createCell(2);
            cell.setCellValue(data.get(i).getEmail());

            cell = row.createCell(3);
            cell.setCellValue(data.get(i).getTypeName());

            if(data.get(i).getTeam() != null){
                cell = row.createCell(4);
                cell.setCellValue(data.get(i).getTeam().getCoach()==1?"是":"否");
                cell = row.createCell(5);
                cell.setCellValue(data.get(i).getTeam().getTeamName());
            }

            cell = row.createCell(6);
            cell.setCellValue(data.get(i).getAreaName());

            cell = row.createCell(7);
            cell.setCellValue(data.get(i).getSourceName());

            if(data.get(i).getMembers()!=null){
                cell = row.createCell(8);
                cell.setCellValue(data.get(i).getMembers().size());
                cell = row.createCell(9);
                StringBuffer sb = new StringBuffer();
                for(TeamMember tm:data.get(i).getMembers()){
                    sb.append(tm.getMemberName()).append(",").append(tm.getMemberPhone()).append(",").append(tm.getMemberEmail()).append(";");
                }
                if(sb.length()>0){
                    sb.deleteCharAt(sb.length()-1);
                }
                cell.setCellValue(sb.toString());
            }

        }

        // 导出Excel文件
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "employee.xlsx");

        return new ResponseEntity<>(outputStream.toByteArray(), headers, org.springframework.http.HttpStatus.CREATED);
    }
}
